// pages/discover/discover.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		totalList: [],
		list: {
			lists: [],
		},
		beginCount: 0,
		endCount: 10,
	},

	getData() {
		wx.request({
			url: 'http://localhost:3000/toplist',
			success: res => {
				this.data.totalList = res.data.list
				this.setData({
					totalList: this.data.totalList
				})
				this.data.list.lists = this.data.totalList.slice(0, this.data.endCount)
				this.setData({
					list: this.data.list
				})
				this.data.list.lists.forEach(item => {
					wx.request({
						url: `http://localhost:3000/playlist/detail?id=${item.id}`,
						success: res2 => {
							item.songs = res2.data.playlist.tracks.slice(0, 3)
							this.setData({
								list: this.data.list
							})
						}
					})
				})
				this.setData({
					beginCount: this.data.beginCount + 10,
					endCount: this.data.endCount + 10,
				})
			}
		})
	},

	goToDetail(e) {
		let id = e.currentTarget.dataset.id
		wx.navigateTo({
			url: `../hotMusic/hotMusic?id=${id}`,
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getData()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {
		this.data.list.lists = this.data.totalList.slice(0, this.data.endCount)
		this.setData({
			list: this.data.list
		})
		this.data.list.lists.slice(this.data.beginCount,this.data.endCount).forEach(item => {
			wx.request({
				url: `http://localhost:3000/playlist/detail?id=${item.id}`,
				success: res2 => {
					item.songs = res2.data.playlist.tracks.slice(0, 3)
					this.setData({
						list: this.data.list
					})
				}
			})
		})
		this.setData({
			beginCount: this.data.beginCount + 10,
			endCount: this.data.endCount + 10,
		})
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})